package com.liy.framework.common.token;

import java.util.*;

import com.liy.framework.common.extvo.LoginInfo;
import com.liy.framework.common.utils.Tools;
import io.jsonwebtoken.impl.*;
import io.jsonwebtoken.*;

public class JwtTokenUtil
{
    public static void main(final String[] args) throws Exception {
        long s = System.currentTimeMillis();
        final LoginInfo loginUser = new LoginInfo();
        loginUser.setId("abc111111111111");
        final String token = newToken(loginUser);
        System.out.println("\u751f\u6210\u65f6\u95f4" + (System.currentTimeMillis() - s));
        System.out.println(token);
        s = System.currentTimeMillis();
        parseToken(token);
        System.out.println("\u89e3\u6790\u65f6\u95f4" + (System.currentTimeMillis() - s));
        s = System.currentTimeMillis();
        System.out.println(Tools.getNewUUID());
        System.out.println("UUID\u751f\u6210\u65f6\u95f4" + (System.currentTimeMillis() - s));
        System.out.println(parseToken(token));
    }

    public static String newToken(final LoginInfo loginUser) {
        final Claims claims = null;
        final JwtBuilder builder = Jwts.builder().setAudience(loginUser.getId()).setClaims(claims).setIssuedAt(new Date()).setNotBefore(new Date()).signWith(SignatureAlgorithm.HS256, Base64Codec.BASE64.decode("password"));
        return builder.compact();
    }

    public static Claims parseToken(final String jwt) throws Exception {
        final Jws<Claims> jws = (Jws<Claims>)Jwts.parser().setSigningKey(Base64Codec.BASE64.decode("password")).parseClaimsJws(jwt);
        return (Claims)jws.getBody();
    }
}
